import CONFIG from "../config/config";
import axios from "axios";
import Qs from "qs";
import qwer from "./asdf"

const user={
  namespaced: true,
  state:()=>{
    return{
      type_id: 0,
      user_id:'',
      is_login:false,
      user_info:{
        user_name:'',
        tel:'',
        email:'',
        user_portrait: '',
        wallet:0,
      },
      address:[{
        address_id:'123',
        name:'home',
        tel:'123123123',
        country:'china',
        address:'34th asdf St.',
        zip_code:'123123',
        is_default:true
      }],

    }
  },
  mutations:{
    loadNewAvatar(state,base){
      console.log(base);
      state.user_info.user_portrait=base;
    }
  },
  actions:{
    login({ state, commit, rootState },obj){
      console.log('登录操作');
      // console.log(Qs.stringify(obj));

      console.log(this.type_id)
      axios.defaults.withCredentials = true // 让ajax携带cookie

      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          withCredentials: true,
          url: '/login/login',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          console.log(res.data);
          // state.type_id=res.data['type_id'];
          state.user_id=res.data['user_id'];
          // console.log('vuex内type_id'+state.type_id);

          state.is_login=true;
          resolve(res);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    register({ state, commit, rootState },obj){
      console.log('注册操作');
      return new Promise((resolve, reject) => {
        axios({
          method: 'post',
          withCredentials: true,
          url: '/login/register',
          data: Qs.stringify(obj),
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          resolve(res);
        }).catch(e=>{
          reject(e);
        })
      })
    },
    userDataQuery({state,commit,rootState}){
      return new Promise(((resolve, reject) => {
        axios.get('/user_center/info').then((res)=>{
          console.log('用户资料');
          console.log(res.data);
          state.user_info.user_name=res.data.user_name;
          state.user_info.user_portrait=res.data.user_portrait;
          state.user_info.email=res.data.email;
          state.user_info.tel=res.data.tel;
          resolve();
        }).catch(e=>{
          reject(e);
        })

      }))
    },
    userDataModify({ state, commit, rootState }){
      // console.log(qwer.portrait);
      return new Promise(((resolve, reject) => {
        console.log(JSON.stringify({
          user_name:state.user_info.user_name,
          tel:state.user_info.tel,
          email:state.user_info.email
        }))
        axios.post('/user_center/modifyinfo',
          Qs.stringify({
            user_name:state.user_info.user_name,
            tel:state.user_info.tel,
            user_portrait:state.user_info.user_portrait,
            email:state.user_info.email
          }),
          {
            headers: {
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
          }).then(()=>{
            resolve()
        }).catch(e=>{
          reject(e);
        })
      }))
    },
    queryCash({ state, commit, rootState }){
      axios.post('/user_center/cash','',{
        headers: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
        }
      }).then((res)=>{
        state.user_info.wallet=res.data.wallet
      }).catch(e=>{
        console.log(e);
      })
    },
    rechargeCash({ state, commit, rootState }){
      /**
       *  TODO:余额充值
       */

    },
    queryAddress({ state, commit, rootState }){
      axios.get('/user_center/address',).then((res)=>{
        console.log(res.data);
        state.address.splice(0,state.address.length,...res.data);
      }).catch(e=>{
        console.log(e);
      })
    },
    modifyAddress({ state, commit, rootState },obj){
      return new Promise(((resolve, reject) => {
        axios.post('/user_center/modifyaddress',Qs.stringify(obj),{
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          resolve();
        }).catch(e=>{
          console.log(e);
          reject(e);
        })
      }))


    },
    addAddress({state,commit,rootState},obj){
      return new Promise((resolve, reject) => {
        axios.post('/user_center/createaddress',Qs.stringify(obj),{
          headers: {
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
          }
        }).then((res)=>{
          resolve();
        }).catch(e=>{
          console.log(e);
          reject(e);
        })
      })

    },
  }
}

export default user;
